	$(() => {
		let show = document.querySelector(".oncontextmenu")
		let city = "大同"
		let btn = [...document.querySelectorAll(".oncontextmenu div")]
		document.addEventListener("contextmenu", function(even) {
			even = even || window.event
			even.preventDefault()
			let x = even.clientX
			let y = even.clientY
			let scrollY = document.documentElement.scrollTop
			let scrollX = document.documentElement.scrollLeft
			show.style.left = x + scrollX + "px"
			show.style.top = y + scrollY + 'px'
			showInit()
		}, false)
		document.addEventListener("click", function() {
			hideInit()
		}, false)

		function showInit() {
			show.style.display = 'block'
		}

		function hideInit() {
			show.style.display = 'none'
		}

		for (let i in btn) {
			btn[i].addEventListener("click", function() {
				changeInit(i)
			}, false)
		}

		function changeInit(index) {
			switch (parseInt(index)) {
				case 0:
					let arr = ["相遇即是缘分", "漂流过海来找你", '只是一个小朋友']
					alert(arr[(Math.random() * 3) | 0])
					break;
				case 1:
					city = prompt("请输入城市名:")
					if (city) {
						wether()
					}
					break;
				case 2:
					window.location.href = history.go(1)
					break;
				case 3:
					window.location.href = history.go(-1)
					break;
				case 4:
					location.reload()
					break;
			}
		}

		let imgArr = ["多云.png", "霾.png", '太阳.png', '下雨.png', '下雪.png']
		$("#city")[0].innerHTML = `
			<div class="img">
							<img src="位置.png" >
			</div>
					${city}`

		wether()
		// 利用promise解决异步
		function wether() {
			new Promise((resovled, rejected) => {
				$.ajax({
					url: "http://www.tianqiapi.com/api?version=v9&appid=23035354&appsecret=8YvlPNrz",
					type: "GET",
					data: {
						appid: "26891764",
						appsecret: "SV5XuXWi",
						version: "v9",
						city: `${city}`
					},
					success: resovled,
					error: rejected
				})
			}).then((res) => {
				$(".status").html(`${res.data[0].air_tips.split('，')[0]}`)
				$("#s1").html(`${res.data[0].wea}`)
				$("#s2").html(`${res.data[0].tem1}°`)
				$("#s3").html(`${res.data[0].tem2}°`)
				$("#now").html(`${res.data[0].tem}°`)
				$("#city").html(`
					<div class="img">
						<img src="位置.png" >
					</div>
					${city}
					`)
				tianqi(res.data[0].wea)
			}).catch((err) => {
				alert("天气获取失败,404,请联系新新小朋友")
			})
		}

		function tianqi(res) {
			if (res.indexOf("多云") >= 0) {
				$("#change")[0].src = imgArr[0]
			} else if (res.indexOf("霾") >= 0) {
				$("#change")[0].src = imgArr[1]
			} else if (res.indexOf("晴") >= 0) {
				$("#change")[0].src = imgArr[2]
			} else if (res.indexOf("小雨") >= 0 || res.indexOf("雨") >= 0 || res.indexOf("大雨") >= 0) {
				$("#change")[0].src = imgArr[3]
			} else if (res.indexOf("小雪") >= 0 || res.indexOf("雪") >= 0 || res.indexOf("大雪") >= 0) {
				$("#change")[0].src = imgArr[4]
			}
		}
	})
